//
//  EMTool.m
//  EMMODaliy
//
//  Created by karry on 2020/4/29.
//  Copyright © 2020 miki. All rights reserved.
//

#import "EMTool.h"
#import<StoreKit/StoreKit.h>
//#import "StepSlider.h"
//#import "EMSmileTodoView.h"

@implementation EMTool

+ (void)showToast:(NSString *)toast{
    [[UIWindow keyWindow] makeToast:toast];
}

+ (void)showAlert:(NSString *)title msg:(NSString *)msg completion:(void(^)(void))completion{
 
}


//+ (BOOL)showSmileTodoGuide{
//    
//    BOOL ret = [[NSUserDefaults standardUserDefaults]boolForKey:@"Key_ifHaveShowSmileTodoGuide"];
//
//    if (ret == YES) {
//        return NO;
//    }
//
//    NSInteger count = [MODiary bg_count:[MODiary mo_tableName] where:nil];
//
//    if (count < 6) {
//        return NO;
//    }
//
//    [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"Key_ifHaveShowSmileTodoGuide"];
//
//    UIView *view = [UIView new];
//    view.frame = CGRectMake(0, 0, [MOAlertBaseView alertWidth], 140);
//    
//    EMSmileTodoView *todoView = [EMSmileTodoView jk_loadInstanceFromNib];
//    todoView.lab0.text = Localizable(@"k_160");
//    todoView.lab1.text = Localizable(@"k_161");
//    [view addSubview:todoView];
//    
//    [todoView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.top.bottom.mas_offset(0);
//    }];
//    
//    MOAlertBaseView *alertView = [MOAlertBaseView alertWithTitle:Localizable(@"k_159") detailTitle:nil contentView:view leftTitle:Localizable(@"k_163") rightTitle:Localizable(@"k_162")];
//    alertView.hiddeAd = YES;
//    alertView.tapDissmss = NO;
//    
//    [alertView.leftBtn setTitleColor:UIColor.grayColor forState:0];
//    [alertView.leftBtn.titleLabel setFont:[UIFont systemFontOfSize:14 weight:UIFontWeightBold]];
//    [alertView.rightBtn setTitleColor:UIColor.mo_blueColor forState:0];
//    [alertView.rightBtn.titleLabel setFont:[UIFont systemFontOfSize:16 weight:UIFontWeightBold]];
//    
//    alertView.didClickIndexBlock = ^(NSInteger index) {
//      
//        if (index == 1) {
//            
//            [EMFeedBackTool feedBack];
//            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://apps.apple.com/app/id%@",@"1551532207"]];
//            
//            [UIApplication.sharedApplication openURL:url options:@{} completionHandler:nil];
//        }
//        
//    };
//    
//    [alertView show];
//    
//    AudioServicesPlaySystemSound(1518);
//    
//    return YES;
//    
//    
//}


+ (void)showScore{
    
//    NSInteger count = [MODiary bg_count:[MODiary mo_tableName] where:nil];
//    if (count >= 2) {
//        if (![self requestReviewIfNeeded]) {
//            return;
//        }
//        [self showSmileTodoGuide];
//    }
    

//    if ([self showSmileTodoGuide]) {
//        return;
//    }
//    
//    BOOL ret = [[NSUserDefaults standardUserDefaults]boolForKey:@"Key_ifHavePraiserecommended"];
//
//    if (ret == YES) {
//        return;
//    }
//
//    NSInteger count = [MODiary bg_count:[MODiary mo_tableName] where:nil];
//    
//    if (count < 2) {
//        return;
//    }
//    
//    [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"Key_ifHavePraiserecommended"];
//    
//    MOAlertBaseView *alert = [MOAlertBaseView alertWithTitle:Localizable(@"k_60") detailTitle:Localizable(@"k_61") contentView:nil leftTitle:Localizable(@"k_62")rightTitle:Localizable(@"k_63")];
//    alert.hiddeAd = YES;
//    alert.didClickIndexBlock = ^(NSInteger index) {
//        if (index == 0) {
//
//            [[UIWindow keyWindow] makeToast:Localizable(@"k_64")];
//            
//        }else if (index == 1){
//
//            [[UIWindow keyWindow] endEditing:YES];
//            [SKStoreReviewController requestReview];
//        }
//    };
//    
//    [alert show];
    
}


+ (BOOL)requestReviewIfNeeded {
    // 获取NSUserDefaults的实例
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    // 尝试读取上一次请求评分的日期
    NSDate *lastRequestedDate = [defaults objectForKey:@"lastReviewRequestDate"];
    
    // 获取当前日期
    NSDate *now = [NSDate date];
    
    // 如果从未请求过评分，或者上次请求与现在的时间差超过一个月，则请求评分
    if (!lastRequestedDate || [now timeIntervalSinceDate:lastRequestedDate] > (30 * 24 * 60 * 60)) {
        // iOS 10.3及以上版本使用SKStoreReviewController请求评分
        [SKStoreReviewController requestReview];
        // 更新上次请求评分的日期
        [defaults setObject:now forKey:@"lastReviewRequestDate"];
        [defaults synchronize];
        return YES;
    }
    return NO;
}



+ (BOOL)isChineseLanguages{
    
    NSArray *langs = [NSLocale preferredLanguages];
    NSString *c_l = langs.firstObject;
    if ([c_l hasPrefix:@"zh"]) {
        return YES;
    }
    return NO;
    
}




@end
